Completed
Push — phpunit ( d6e4f3...02d783 )
by Sander
15:22 queued 04:51
created

angular.controller(ꞌSettingsCtrlꞌ)   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 209

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
dl 0
loc 209
rs 8.1463
c 6
b 0
f 0
cc 6
nc 13
nop 11

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
(function () {
2
	'use strict';
3
4
5
	/**
6
	 * @ngdoc function
7
	 * @name passmanApp.controller:SettingsCtrl
8
	 * @description
9
	 * # SettingsCtrl
10
	 * Controller of the passmanApp
11
	 */
12
	angular.module('passmanApp')
13
		.controller('SettingsCtrl', ['$scope', '$rootScope', 'SettingsService', 'VaultService', 'CredentialService', '$location', '$routeParams', '$http', 'EncryptService', 'NotificationService', '$sce',
14
			function ($scope, $rootScope, SettingsService, VaultService, CredentialService, $location, $routeParams, $http, EncryptService, NotificationService, $sce) {
15
				$scope.vault_settings = {};
16
				$scope.new_vault_name = '';
17
				$scope.active_vault = VaultService.getActiveVault();
18
				if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
19
					if (!$scope.active_vault) {
20
						$location.path('/');
21
						return;
22
					}
23
				} else {
24
					if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
25
						var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
26
						_vault.vaultKey = SettingsService.getSetting('defaultVaultPass');
27
						VaultService.setActiveVault(_vault);
28
						$scope.active_vault = _vault;
29
					}
30
				}
31
32
				VaultService.getVault($scope.active_vault).then(function (vault) {
33
					vault.vaultKey = VaultService.getActiveVault().vaultKey;
34
					delete vault.credentials;
35
					VaultService.setActiveVault(vault);
36
					$scope.vault_settings = vault.vault_settings;
37
					if(!$scope.vault_settings.hasOwnProperty('pwSettings')){
38
						$scope.vault_settings.pwSettings = {
39
							'length': 12,
40
							'useUppercase': true,
41
							'useLowercase': true,
42
							'useDigits': true,
43
							'useSpecialChars': true,
44
							'minimumDigitCount': 3,
45
							'avoidAmbiguousCharacters': false,
46
							'requireEveryCharType': true,
47
							'generateOnCreate': true
48
						};
49
					}
50
				});
51
52
53
54
				var http = location.protocol, slashes = http.concat("//"), host = slashes.concat(window.location.hostname), complete = host + location.pathname;
55
				$scope.bookmarklet = $sce.trustAsHtml("<a class=\"button\" href=\"javascript:(function(){var a=window,b=document,c=encodeURIComponent,e=c(document.title),d=a.open('" + complete + "bookmarklet?url='+c(b.location)+'&title='+e,'bkmk_popup','left='+((a.screenX||a.screenLeft)+10)+',top='+((a.screenY||a.screenTop)+10)+',height=750px,width=475px,resizable=0,alwaysRaised=1');a.setTimeout(function(){d.focus()},300);})();\">Save in passman</a>");
56
57
58
				$scope.saveVaultSettings = function () {
59
					var _vault = $scope.active_vault;
60
					_vault.name = $scope.new_vault_name;
61
					_vault.vault_settings = angular.copy($scope.vault_settings);
62
					VaultService.updateVault(_vault).then(function () {
63
						//VaultService.setActiveVault(_vault);
64
						$scope.active_vault.name = angular.copy(_vault.name);
65
						NotificationService.showNotification('Settings saved', 5000);
66
					});
67
				};
68
69
70
				$scope.tabs = [
71
					{
72
						title: 'General settings',
73
						url: 'views/partials/forms/settings/general_settings.html'
74
					},
75
					{
76
						title: 'Password Audit',
77
						url: 'views/partials/forms/settings/tool.html'
78
79
					},
80
					{
81
						title: 'Password settings',
82
						url: 'views/partials/forms/settings/password_settings.html'
83
84
					},
85
					{
86
						title: 'Import credentials',
87
						url: 'views/partials/forms/settings/import.html'
88
89
					},
90
					{
91
						title: 'Export credentials',
92
						url: 'views/partials/forms/settings/export.html'
93
94
					},
95
					{
96
						title: 'Sharing',
97
						url: 'views/partials/forms/settings/sharing.html'
98
					}
99
				];
100
101
				$scope.currentTab = $scope.tabs[0];
102
103
				$scope.onClickTab = function (tab) {
104
					$scope.currentTab = tab;
105
				};
106
107
				$scope.isActiveTab = function (tab) {
108
					return tab.url === $scope.currentTab.url;
109
				};
110
111
				var getPassmanVersion = function () {
112
					var url = OC.generateUrl('apps/passman/api/internal/version');
113
					$http.get(url).then(function (result) {
114
						$scope.passman_version = result.data.version;
115
					});
116
				};
117
				getPassmanVersion();
118
119
				$scope.$watch(function () {
120
					return VaultService.getActiveVault();
121
				}, function (vault) {
122
					if (vault) {
123
						$scope.active_vault = vault;
124
					}
125
				});
126
127
				$rootScope.$on('logout', function () {
128
					$scope.selectedVault = false;
129
				});
130
				$scope.startScan = function (minStrength) {
131
					VaultService.getVault($scope.active_vault).then(function (vault) {
132
						var results = [];
133
						for (var i = 0; i < vault.credentials.length; i++) {
134
							var c = angular.copy(vault.credentials[i]);
135
							if (c.password && c.hidden === 0) {
136
								c = CredentialService.decryptCredential(c);
137
								if (c.password) {
138
									var zxcvbn_result = zxcvbn(c.password);
139
									if (zxcvbn_result.score <= minStrength) {
140
										results.push({
141
											credential_id: c.credential_id,
142
											label: c.label,
143
											password: c.password,
144
											password_zxcvbn_result: zxcvbn_result
145
										});
146
									}
147
								}
148
149
							}
150
							//@todo loop custom fields (if any and check secret fields
151
						}
152
						$scope.scan_result = results;
153
					});
154
				};
155
156
157
				$scope.cur_state = {};
158
159
				$scope.changeVaultPassword = function (oldVaultPass, newVaultPass, newVaultPass2) {
160
					if (oldVaultPass !== VaultService.getActiveVault().vaultKey) {
161
						$scope.error = 'Your old password is incorrect!';
162
						return;
163
					}
164
					if (newVaultPass !== newVaultPass2) {
165
						$scope.error = 'New passwords do not match!';
166
						return;
167
					}
168
					VaultService.getVault($scope.active_vault).then(function (vault) {
169
						var _selected_credentials = [];
170
						if (vault.credentials.length === 0) {
171
							$location.path('/');
172
						}
173
						for (var i = 0; i < vault.credentials.length; i++) {
174
							var _credential = vault.credentials[i];
175
							if (_credential.shared_key === null || _credential.shared_key === '') {
176
								_selected_credentials.push(_credential);
177
							}
178
						}
179
						$scope.change_pw = {
180
							percent: 0,
181
							done: 0,
182
							total: _selected_credentials.length
183
						};
184
						var changeCredential = function (index, oldVaultPass, newVaultPass) {
185
							CredentialService.reencryptCredential(_selected_credentials[index].guid, oldVaultPass, newVaultPass).progress(function (data) {
186
								$scope.cur_state = data;
187
							}).then(function () {
188
								var percent = index / _selected_credentials.length * 100;
189
								$scope.change_pw = {
190
									percent: percent,
191
									done: index + 1,
192
									total: _selected_credentials.length
193
								};
194
								if (index < _selected_credentials.length - 1) {
195
									changeCredential(index + 1, oldVaultPass, newVaultPass);
196
								} else {
197
									vault.private_sharing_key = EncryptService.decryptString(angular.copy(vault.private_sharing_key), oldVaultPass);
198
									vault.private_sharing_key = EncryptService.encryptString(vault.private_sharing_key, newVaultPass);
199
									VaultService.updateSharingKeys(vault).then(function () {
200
										$rootScope.$broadcast('logout');
201
										NotificationService.showNotification('Please login with your new vault password', 5000);
202
									});
203
								}
204
							});
205
						};
206
						changeCredential(0, VaultService.getActiveVault().vaultKey, newVaultPass);
207
208
					});
209
				};
210
211
				$rootScope.$on('logout', function () {
212
					$scope.active_vault = null;
213
					VaultService.setActiveVault(null);
214
					$location.path('/');
215
216
				});
217
218
				$scope.cancel = function () {
219
					$location.path('/vault/' + $routeParams.vault_id);
220
				};
221
222
			}]);
223
224
}());